package com.MRSyc.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.MRSyc.config.PageModel;
import com.MRSyc.entity.TDept;
import com.MRSyc.entity.TUserinfo;
import com.MRSyc.mapper.TDeptMapper;
import com.MRSyc.mapper.TUserinfoMapper;
import com.MRSyc.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.abel533.sql.SqlMapper;
import com.google.gson.Gson;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Fxq
 * @since 2021-01-12
 */
@Slf4j
@Controller
public class TDeptController {
	@Autowired
	private TDeptMapper deptMapper;

	@Autowired
	private TUserinfoMapper userMapper;

	@RequestMapping(value = "/dept/list", method = { RequestMethod.GET })
	public String dept_list() throws Exception {
		return "dept/dept_list";
	}

	@RequestMapping(value = "/dept/list", method = { RequestMethod.POST })
	@ResponseBody
	public PageModel list(TDept dept, PageModel<TDept> page) throws Exception {
		page.init();
		IPage<TDept> ipage = new Page<>(page.getCurrentPageNum(), page.getPageSize());
		QueryWrapper<TDept> queryWrapper = new QueryWrapper<>();
		IPage<TDept> list = deptMapper.selectPage(ipage, queryWrapper);

		page.packData(list.getTotal(), list.getRecords());
		return page;
	}

	@RequestMapping(value = "/dept/add", method = { RequestMethod.GET })
	public String addInit(TDept dept, Model model) {
		return "dept/dept_add";
	}

	@RequestMapping(value = "/dept/add", method = { RequestMethod.POST })
	@ResponseBody
	public Result add(TDept dept) {
		dept.setId(deptMapper.getMaxId());
		deptMapper.insert(dept);
		return Result.genSuccessResult();
	}

	@RequestMapping(value = "/dept/update", method = { RequestMethod.GET })
	public String updateInit(TDept dept, Model model) {
		dept = deptMapper.selectById(dept.getId());
		model.addAttribute("dept", dept);
		return "dept/dept_update";
	}

	@RequestMapping(value = "/dept/update", method = { RequestMethod.POST })
	@ResponseBody
	public Result update(TDept dept) {
		deptMapper.updateById(dept);
		return Result.genSuccessResult();
	}

	@RequestMapping(value = "/dept/delete", method = { RequestMethod.POST })
	@ResponseBody
	public Result delete(TDept dept) {
		TUserinfo record = new TUserinfo();
		record.setDeptId(dept.getId());
		QueryWrapper<TUserinfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.setEntity(record);
		// 查询所有以此部门为部门的用户，若有，则提示不能删除
		List<TUserinfo> findList = userMapper.selectList(queryWrapper);
		if (findList != null && findList.size() > 0) {
			return Result.genResult("998", "本部门有用户在使用，不可删除！");
		}
		deptMapper.deleteById(dept.getId());
		return Result.genSuccessResult();
	}

	@Autowired
	private SqlMapper sqlMapper;

	private Gson gson = new Gson();

	@RequestMapping(value = "/dept/selectPageList", method = { RequestMethod.GET })
	public String selectPageList() {
		return "dept/dept_selectPageList";
	}

	@RequestMapping(value = "/dept/selectPageList", method = { RequestMethod.POST })
	@ResponseBody
	public PageModel selectPageList(TDept dept, PageModel<TDept> page) throws Exception {
		page.init();
		QueryWrapper<TDept> queryWrapper = new QueryWrapper<>();
		queryWrapper.setEntity(dept);
		IPage<TDept> ipage = new Page<>(page.getCurrentPageNum(), page.getPageSize());
		IPage<TDept> list = deptMapper.selectPage(ipage, queryWrapper);
		page.packData(list.getTotal(), list.getRecords());
		return page;
	}

	@RequestMapping(value = "/dept/choosePage", method = { RequestMethod.GET })
	public String selectPage(String fkName, Model model) {
		if (StringUtils.isNotEmpty(fkName)) {
			Map<String, Object> foreignKeyMap = sqlMapper
					.selectOne("select * from t_fkeys where fkName = \'" + fkName + "\'");
			String rSql = foreignKeyMap.get("rSql").toString();
			log.info("rSql:{}", rSql);
			List<Map<String, Object>> relativeMapList = sqlMapper.selectList(rSql);
			if (relativeMapList != null) {
				model.addAttribute("relativeMapList", relativeMapList);
				String rType = foreignKeyMap.get("rType").toString();
				if ("0".equals(rType))
					return select_radioPage(model);
				if ("1".equals(rType))
					return select_radioPage(model);
				if ("2".equals(rType)) {
					String coverOtherValueColumn = foreignKeyMap.get("coverOtherValueColumn").toString();
					model.addAttribute("coverOtherValueColumn", coverOtherValueColumn);
					model.addAttribute("treeNodes", toJson(relativeMapList));
					return select_zTreePage(model);
				}
			} else {
				return "error/error";
			}
		}
		return "error/error";
	}

	private String select_radioPage(Model model) {
		model.addAttribute("demo", "demoValue");
		return "dept/dept_selectPageList";
	}

	private String select_zTreePage(Model model) {
		model.addAttribute("demo", "demoValue");
		return "dept/dept_foreignKeyTree";
	}

	public String toJson(Object obj) {
		String s = gson.toJson(obj);
		return s;
	}

}
